package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.date.DateUtil;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.object.pojo.CodeChangeLog;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.CodeChangeLogService;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: chen
 * @Description: 二码绑定日志
 * @Date: 创建于 17:12 2020/8/28
 */
@RestController
@RequestMapping("/codeChangeLog")
public class CodeChangeLogController {
    @Autowired
    private CodeChangeLogService codeChangeLogService;

    /**
     * 分页查询
     *
     * @param accountId 区域id
     * @param key       新旧车辆编号或设备编号
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param pageNO    页码数 默认1
     * @param rowCount  每页大小 默认10
     * @return
     */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(String accountIds, String key, Long startTime, Long endTime, @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount, Integer type) {
        List<CodeChangeLog> logs = codeChangeLogService.queryPage(accountIds, key, startTime, endTime, pageNO, rowCount, type);
        Integer total = codeChangeLogService.queryCount(accountIds, key, startTime, endTime, type);
        return JsonResult.ok(logs, total);
    }


    /**
     * 二码绑定日志导出
     *
     * @param accountIds 区域id
     * @param key       新旧车辆编号或设备编号
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @PostMapping("/exportCodeChangeLog")
    public Result<Object> exportCodeChangeLog(String accountIds, String key, Long startTime, Long endTime, Integer type) {
        List<CodeChangeLog> logs = codeChangeLogService.queryPage(accountIds, key, startTime, endTime, 1, 65535, type);
        for (CodeChangeLog log : logs) {
            log.setExportUserCode(log.getOldUserCode() + "->" + log.getNewUserCode());
            log.setExportMachineNO(log.getOldMachineNO() + "->" + log.getNewMachineNO());
        }
        String fileName = "二码绑定日志";
        if (Objects.nonNull(startTime) && Objects.nonNull(endTime)){
            if (DateUtil.betweenMonth(new Date(startTime), new Date(endTime), true) > 6){
                return Result.error("仅允许导出6个月的数据");
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            fileName = String.format("%s~%s 二码绑定日志", sdf.format(new Date(startTime)), sdf.format(new Date(endTime)));
        }
        ExcelUtil<CodeChangeLog> excelUtil = new ExcelUtil<>(CodeChangeLog.class);
        JsonResult result = excelUtil.exportExcel(logs, fileName);
        return Result.success(result.getData());
    }
}
